import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class View {
  boolean loop = true;
  String Key;
  Scanner input = new Scanner(System.in);
  //持有service
  Service service = new Service();

  //修改学生成绩

  void AlterStudent() {
    System.out.println("===============修改学生成绩===========");
    System.out.println("!输入-9退出修改(-1)跳过修改项)");
    System.out.print("学号/姓名:");
    String snoORName = Utility.readString(6, "");
    if (snoORName.equals("-9")) {
      System.out.println("============！！！你已取消修改学生成绩===========");
      return;
    }
    //拿到对象
    Student student = service.find(snoORName);
    if (student == null) {
      System.out.println("============该学生不存在，核对后重新输入==========");
      return;
    }
    System.out.print("姓名(" + student.getName() + "):");
    String name = Utility.readString(4, "");
    if (name.equals("-9")) {
      System.out.println("============！！！你已取消修改学生成绩===========");
      return;
    }
    //把输入的数据一次性修改


    System.out.print("数学(" + student.getMath() + "):");

    Double Math = input.nextDouble();
    if (Math == -9) {
      System.out.println("============！！！你已取消修改学生成绩===========");
      return;
    }

    if (!(Math >= -9 && Math <= 100)) {
      System.out.println("==========！！修改失败 你的分数输入有误(1~100)===========");
      return;
    }

    System.out.print("计算机(" + student.getComputer() + "):");
    Double Computer = input.nextDouble();
    if (Computer == -9) {
      System.out.println("============！！！你已取消修改学生成绩===========");
      return;
    }
    if (!(Computer >= -9 && Computer <= 100)) {
      System.out.println("==========！！修改失败 你的分数输入有误(1~100)===========");
    }
    System.out.print("安卓(" + student.getAndroid() + "):");

    Double Android = input.nextDouble();
    if (Android == -9) {
      System.out.println("============！！！你已取消修改学生成绩===========");
      return;
    }
    if (!(Android >= -9&& Android <= 100)) {
      System.out.println("==========！！修改失败 你的分数输入有误(1~100)===========");
      return;
    }
    //真修改

    if (name != "-1") student.setName(name);

    if (Math != -1.0) student.setMath(Math);
    if (Computer != -1.0) student.setComputer(Computer);
    if (Android != -1.0) student.setAndroid(Android);
    //service.update();
    System.out.println("===============修改成功===========");

  }

  //查找学生 id和姓名都可以
  void findStudentBySnoOrName() {
    System.out.println("===============查找学生成绩===========");
    System.out.println("！回车取消查找");
    System.out.print("姓名/学号:");
    String snoORName = Utility.readString(6, "");
    if (snoORName.equals("")) {
      System.out.println("===============你已取消查找学生信息===========");
    }
    //拿到学生
    Student student = service.find(snoORName);
    if (student != null) {
      System.out.println("===============显示" + student.getName() + "同学的成绩===========");
      System.out.println("学号\t姓名\t数学\t计算机\t安卓\t");
      System.out.println(student);
      System.out.println("===============显示完毕===========");
    } else {
      System.out.println("========该学生不存在===========");
    }

  }

  //查看排序后的结果
  public void sort() {
    System.out.println("===============排序成绩（降序）===========");
    List<Student> students = Student.sortBySum(service.getStudents());
    System.out.println("姓名\t数学\t计算机\t安卓\t总分");
    for (int i = 0; i < students.size(); i++) {
      Student student = students.get(i);
      System.out.println(student.getName() + "\t" + student.getMath() + "\t" + student.getComputer() + "\t" + student.getAndroid() + "\t" + student.getTotalScore());
    }
    System.out.println("===============显示完毕===========");
  }


  //删除学生 id和姓名都可以
  void deleteStudent() {
    System.out.println("===============删除学生成绩===========");
    System.out.println("！回车取消删除");
    System.out.print("id/学号:");
    String snoORName = Utility.readString(6, "");
    if (snoORName.equals("")) {
      System.out.println("===============你已取消删除学生信息===========");
    }
    //真要删？
    System.out.print("！确认是否删除(Y/N):");
    if (Utility.readConfirmSelection() == 'Y') {
      if (service.remove(snoORName)) {
        System.out.println("===============删除学生信息成功===========");
      } else {
        System.out.println("========删除失败,该学生不存在===========");
      }
    } else {
      System.out.println("===============你已取消删除学生信息===========");
    }

  }

  //显示所有的显示
  void showAllStudent() {
    ArrayList<Student> students = service.getStudents();
    System.out.println("===============显示所有学生成绩===========");
    System.out.println("学号\t姓名\t数学\t计算机\t安卓\t");
    for (int i = 0; i < students.size(); i++) {
      System.out.println(students.get(i));
    }
    System.out.println("===============显示完毕===========");
  }


  //添加学生
  void add() {
    System.out.println("===============添加学生成绩===========");
    System.out.println("!回车键取消操作");
    System.out.print("姓名:");
    String name = Utility.readString(4, "");
    if (name.equals("")) {
      System.out.println("============！！！你已取消添加学生成绩===========");
      return;
    }
//    数学成绩Maths, 计算机基础成绩Computer,安卓成绩Android
    System.out.print("数学:");
    Double Math = input.nextDouble();
    if (!(Math >= 0 && Math <= 100)) {
      System.out.println("============！！！你的分数输入有误(1~100)===========");
      return;
    }
    System.out.print("计算机:");
    Double Computer = input.nextDouble();
    if (!(Computer >= 0 && Computer <= 100)) {
      System.out.println("============！！！你的分数输入有误(1~100)===========");
      return;
    }
    System.out.print("安卓:");
    Double Android = input.nextDouble();
    if (!(Android >= 0 && Android <= 100)) {
      System.out.println("==========！！添加失败 你的分数输入有误(1~100)===========");
      return;
    }
    //真添加
    if (service.add(new Student(name, Math, Computer, Android))) {
      System.out.println("===============添加成功===========");
    } else {
      System.out.println("===============添加失败===========");
    }

  }

  void mainMenu() {
    do {

      System.out.println("===============班级学生成绩管理系统===========");
      System.out.println("\t\t 1 显示所有学生信息");
      System.out.println("\t\t 2 添加学生信息");
      System.out.println("\t\t 3 删除学生信息");
      System.out.println("\t\t 4 查找学生成绩");
      System.out.println("\t\t 5 修改学生成绩");
      System.out.println("\t\t 6 查看排序成绩");
      System.out.println("\t\t 7 退出管理系统");
      System.out.println("请输入选择:");
      Key = Utility.readString(32);
      switch (Key) {
        case "1":
          this.showAllStudent();
          break;
        case "2":
          this.add();
          break;
        case "3":
          this.deleteStudent();
          break;
        case "4":
          findStudentBySnoOrName();
          break;
        case "5":
          AlterStudent();
          break;
        case "6":
          sort();
          break;
        case "7":
          System.out.print("！确认是否退出(Y/N):");
          if (Utility.readConfirmSelection() == 'Y') {
            System.out.println("===============你已退出学生管理系统===========");
            loop = false;
          } else {
            System.out.println("===============你取消退出了学生管理系统===========");
          }
          break;
        default:
          System.out.println("输入有误");

      }
    } while (loop);

  }
}
